package com.huixue.cn.ztej.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huixue.cn.ztej.bean.ConstructionTeam;
import com.huixue.cn.ztej.bean.EmployApply;
import com.huixue.cn.ztej.bean.EmployHour;
import com.huixue.cn.ztej.bean.Examine;
import com.huixue.cn.ztej.bean.Picture;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.User;
import com.huixue.cn.ztej.enums.RoleStatus;
import com.huixue.cn.ztej.persistence.Domain;
import com.huixue.cn.ztej.service.SysConstructionTeamService;
import com.huixue.cn.ztej.service.SysPictureService;
import com.huixue.cn.ztej.service.SysProjectService;
import com.huixue.cn.ztej.service.impl.EmployApplyServiceImpl;
import com.huixue.cn.ztej.service.impl.EmployHourServiceImpl;
import com.huixue.cn.ztej.service.impl.ExamineServiceImpl;
import com.huixue.cn.ztej.utils.Urls;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.framwork.JacksonUtil;
import com.huixue.cn.ztej.utils.framwork.PageParameter;

@Domain(Examine.class)
@Controller
@RequestMapping("/sys/examine")
public class ExamineController extends BaseController{

	@Autowired
	private EmployApplyServiceImpl employApplyServiceImpl;
	@Autowired
	private ExamineServiceImpl examineServiceImpl;
	@Autowired
	private EmployHourServiceImpl employHourServiceImpl;
	@Autowired
	private SysConstructionTeamService sysConstructionTeamService;
	@Autowired
	private SysPictureService sysPictureService;
	
	@Autowired(required = true)
	SysProjectService sysProjectService;
	
	/**
	*方法描述：跳转到列表页面
	*创建人：fym
	*创建时间：2017年1月10日 17:18:33
	**/
	@RequestMapping("/toListforExamine")
	public String toListforExamine(
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = PageParameter.DEFAULT_PAGE_SIZE+"") Integer rows,
			HttpServletResponse response,HttpServletRequest request,Model model){
		User user = (User)request.getSession().getAttribute("loginUser");
		model.addAttribute("user",user);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Integer> list1 = new ArrayList<Integer>();
		String resultstr ="";
		model.addAttribute("roleId",user.getRoleId());
		
		String visaCode = request.getParameter("visaCode");
		if(visaCode!=null && visaCode.trim().length()>0){
			map.put("visaCode", visaCode.trim());
		}
		
		String proejctId = request.getParameter("projectId");
		if(proejctId!=null && proejctId.trim().length()>0 && Integer.valueOf(proejctId) >0 ){
			map.put("proId", proejctId);
			model.addAttribute("projectId", proejctId);
		}
		
		Results projectRes =  sysProjectService.findAll(map);
		model.addAttribute("projectRes", projectRes);
		
		String startDate = request.getParameter("startTime");
		String endDate = request.getParameter("endTime");
		if(startDate!=null){
			map.put("startDate", startDate);
			model.addAttribute("startDate",startDate);
		}
		
		if(endDate!=null){
			map.put("endDate", endDate);
			model.addAttribute("endDate",endDate);
		}
		
		if(user.getRoleId().equals(RoleStatus.ROLE_4.getRoleId()+"")){   //工段管理者查询
			list1.add(0);
			list1.add(4);
			list1.add(-4);
			map.put("state", list1);
			map.put("segmentId", user.getSegmentID());
			map.put("proId", user.getProjectID());
			Results results = employApplyServiceImpl.searchToExamine(map, page,rows);
			model.addAttribute("res",results);
			resultstr = "/employ/texamine/list";
		}else if(user.getRoleId().equals(RoleStatus.ROLE_5.getRoleId()+"")){   //合同部查询
			list1.add(4);
			list1.add(5);
			list1.add(-5);
			map.put("state", list1);
			//如果不需要根据工段Id查询，只需注释掉查询条件即可
			//map.put("segmentId", user.getSegmentID());
			map.put("proId", user.getProjectID());
			Results results = employApplyServiceImpl.searchToExamine(map, page,rows);
			model.addAttribute("res",results);
			resultstr = "/employ/texamine/list";
		}else if(user.getRoleId().equals(RoleStatus.ROLE_6.getRoleId()+"")){ //项目经理查询
			list1.add(5);
			list1.add(6);
			list1.add(-6);
			map.put("state", list1);
			map.put("proId", user.getProjectID());
			Results results = employApplyServiceImpl.searchToExamine(map, page,rows);
			model.addAttribute("res",results);
			resultstr = "/employ/texamine/list";
		}else if(user.getRoleId().equals(RoleStatus.ROLE_SYSTETM.getRoleId()+"")
				||user.getRoleId().equals(RoleStatus.ROLE_7.getRoleId()+"")
				||user.getRoleId().equals(RoleStatus.ROLE_8.getRoleId()+"")
				||user.getRoleId().equals(RoleStatus.ROLE_9.getRoleId()+"")){
			//map.put("state", null);
			String pstate = request.getParameter("pstate");
			
			if(pstate!=null && !pstate.equals("")){
				if(pstate.contains(",")){
					map.put("state", Arrays.asList(pstate.split(",")));
				}else{
					list1.add(Integer.valueOf(pstate));
					map.put("state", list1);
				}
				model.addAttribute("pstate",pstate);

			}
			
			if(user.getRoleId().equals(RoleStatus.ROLE_8.getRoleId()+"")){
				map.put("proId", user.getProjectID());
			}
			
			Results results = employApplyServiceImpl.searchToExamine(map, page,rows);
			model.addAttribute("res",results);
			resultstr = "/employ/texamine/list";
		}else if(user.getRoleId().equals(RoleStatus.ROLE_2.getRoleId()+"")){
			map.put("applyUserId", user.getUser_id());
			Results results = employApplyServiceImpl.searchToExamine(map, page,rows);
			model.addAttribute("res",results);
			resultstr = "/employ/texamine/list";
		}else{
			resultstr = "/employ/texamine/list";
		}
		
		return  resultstr;
	}
	
	@RequestMapping("/beforeExam")
	public String beforeExam(HttpServletResponse response,HttpServletRequest request,Model model){
		String applyId = request.getParameter("id");
		User user = (User)request.getSession().getAttribute("loginUser");
		model.addAttribute("user",user);
		EmployApply employApply = employApplyServiceImpl.selectById(applyId);
		List<EmployHour> list = employHourServiceImpl.findByApplyId(applyId);
		List<Examine> list1 = examineServiceImpl.selectByApplyId(Integer.parseInt(applyId),1);
		ConstructionTeam ct = sysConstructionTeamService.getBySecId(employApply.getConstructionId());
		Picture picture = new Picture();
		picture.setType(2);
		picture.setApplyId(Integer.parseInt(applyId));
		List<Picture> piclist = sysPictureService.listPicturyByApply(picture);
		model.addAttribute("res",employApply);
		model.addAttribute("list",list);
		model.addAttribute("list1",list1);
		model.addAttribute("ct",ct);
		model.addAttribute("piclist",piclist);
		
		return "/employ/texamine/eaxm";
	}
	
	@RequestMapping("/toExam")
	@ResponseBody
	public Results toExam(HttpServletResponse response,HttpServletRequest request,@RequestBody Examine examine){
		User user = (User)request.getSession().getAttribute("loginUser");
		examine.setExamineTime(new Date());
		examine.setExamineId(user.getUser_id());
		examine.setExamineName(user.getUserName());
		Results results = null;
		if(user.getRoleId().equals("3")){
			results = examineServiceImpl.add(examine);
			EmployApply employApply = new EmployApply();
			employApply.setApplyId(examine.getApplyId());
			if(examine.getState()==1){
				employApply.setState(3);
			}else{
				employApply.setState(-3);
			}
			employApply.setApproveTime(new Date());
			employApply.setApproveId(user.getUser_id());
			employApplyServiceImpl.updateforExam(employApply);
		}else if(user.getRoleId().equals("4")){
			results = examineServiceImpl.add(examine);
			EmployApply employApply = new EmployApply();
			if(examine.getState()==1){
				employApply.setState(4);
			}else{
				employApply.setState(-4);
			}
			employApply.setApproveTime(new Date());
			employApply.setApproveId(user.getUser_id());
			employApplyServiceImpl.updateforExam(employApply);
		}else if(user.getRoleId().equals("5")){
			results = examineServiceImpl.add(examine);
			EmployApply employApply = new EmployApply();
			if(examine.getState()==1){
				employApply.setState(5);
			}else{
				employApply.setState(-5);
			}
			employApply.setApproveTime(new Date());
			employApply.setApproveId(user.getUser_id());
			employApplyServiceImpl.updateforExam(employApply);
		}
		return results;
	}
	
	@RequestMapping("/viewExam")
	public String viewExam(HttpServletResponse response,HttpServletRequest request,Model model){
		String applyId = request.getParameter("id");
		User user = (User)request.getSession().getAttribute("loginUser");
		model.addAttribute("user",user);
		EmployApply employApply = employApplyServiceImpl.selectById(applyId);
		List<EmployHour> list = employHourServiceImpl.findByApplyId(applyId);
		List<Examine> list1 = examineServiceImpl.selectByApplyId(Integer.parseInt(applyId),1);
		ConstructionTeam ct = sysConstructionTeamService.getBySecId(employApply.getConstructionId());
		Picture picture = new Picture();
		picture.setType(2);
		picture.setApplyId(Integer.parseInt(applyId));
		List<Picture> piclist = sysPictureService.listPicturyByApply(picture);
		model.addAttribute("res",employApply);
		model.addAttribute("list",list);
		model.addAttribute("list1",list1);
		model.addAttribute("ct",ct);
		model.addAttribute("piclist",piclist);
		return "/employ/texamine/eaxmview";
	}
	
	@RequestMapping("/updateById/{applyId}")
	@ResponseBody
	public Results updateById(@PathVariable("applyId")Integer applyId,HttpServletResponse response,HttpServletRequest request,Model model){
		EmployApply employApply = employApplyServiceImpl.selectById(String.valueOf(applyId));
		if(employApply.getState()<0){
			employApply.setState(employApply.getState()-10000);
		}
		employApplyServiceImpl.updateById(employApply);
		
		Results results = new Results(
				API_RETURN_STATUS.NORMAL.value(),
				API_RETURN_STATUS.NORMAL.desc()
				);
		System.out.println(new JacksonUtil().getJson(results));
		return results;
	}
	
	@RequestMapping("/print")
	public String print(HttpServletResponse response,HttpServletRequest request,Model model){
		String applyId = request.getParameter("id");
		
		String url = Urls.BASE_REPORT_URL+"/birt/frameset?__report=employ_view.rptdesign&applyId="+applyId;
		System.out.println("===url=="+url);
		//return new ModelAndView(url,"command","2");
		
		return "redirect:"+url;
	}
	

	
}
